﻿using Aliyun.OSS;
using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Web.IService;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using TencentCloud.Tdmq.V20200217.Models;

namespace AntUnion.FrameWork.Web.Service
{
    public class MeunService : IMenuService
    {
        /// <summary>
        /// 通过角色获取菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<Menu> GetMenu(int roleId, UserType userType)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<Menu> menus = new List<Menu>();
                    List<TbMenu> tbMenus = mtContext.TbMenus.Where(k => k.UserType == (int)userType && k.State == 1).ToList();
                    var a = mtContext.TbMenuRoles.Where(k => k.RoleId == roleId && k.UserType == (int)userType).ToList();
                    List<int> menuids = mtContext.TbMenuRoles.Where(k => k.RoleId == roleId && k.UserType == (int)userType).Select(n => new { Id = n.MenuId }).ToList().Select(u => u.Id).ToList<int>();
                    tbMenus = tbMenus.Where(k => menuids.Contains(k.Id)).ToList();
                    menus = tbMenus.Where(k => k.Pid == 0).OrderBy(k => k.Sort).Select(y => new Menu
                    {
                        Id = y.Id,
                        Names = y.Names,
                        Url = y.Url,
                        Image = y.Image,
                        Remark = y.Remark,
                        children = tbMenus.Where(k => k.Pid == y.Id).OrderBy(k => k.Sort).Select(x => new MenuTo
                        {
                            Id = x.Id,
                            Names = x.Names,
                            Url = x.Url,
                            Image = x.Image,
                            Remark = x.Remark
                        }).ToList()
                    }).ToList();
                    return menus;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        public void GetUNRead(int crmId, out int payCount, out int referralCount, out int personCount)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<CrmAuditLog> crmAuditLogs = mtContext.CrmAuditLogs.Where(k => k.CrmId == crmId && k.ReviewerId != null && k.Isread == 0).ToList();
                    personCount= crmAuditLogs.Where(k => k.Type == 1 || k.Type == 2 || k.Type == 3).Count();
                    payCount = crmAuditLogs.Where(k => k.Type == 4 || k.Type == 5 || k.Type == 6).Count();
                    referralCount = crmAuditLogs.Where(k => k.Type == 7).Count();
                }
            }
            catch (System.Exception)
            {
                throw;
            }

        }

        public void UpdateRead(int crmId, int type)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    IQueryable<CrmAuditLog> crmAuditLogs = null;
                    if(type==1)
                    {
                        crmAuditLogs = mtContext.CrmAuditLogs.Where(k => k.CrmId == crmId && k.ReviewerId != null && k.Isread==0 && (k.Type == 1 || k.Type == 2 || k.Type == 3));
                    }
                    else if(type==2)
                    {
                        crmAuditLogs = mtContext.CrmAuditLogs.Where(k => k.CrmId == crmId && k.ReviewerId != null && k.Isread == 0 && (k.Type == 4 || k.Type == 5 || k.Type == 6));
                    }
                    else if (type == 3)
                    {
                        crmAuditLogs = mtContext.CrmAuditLogs.Where(k => k.CrmId == crmId && k.ReviewerId != null && k.Isread == 0 && (k.Type == 7));
                    }
                    else
                    {

                    }
                    // 批量更新记录
                    foreach (var entcrmAuditity in crmAuditLogs)
                    {
                        entcrmAuditity.Isread = 1;
                    }
                    mtContext.UpdateRange(crmAuditLogs);
                    mtContext.SaveChanges();
                }
            }
            catch (System.Exception)
            {
                throw;
            }

        }
    }
}
